package com.topscomm.pm.script;

import com.topscomm.basic.BasicEntity;
import com.topscomm.basic.exception.BusinessException;
import com.topscomm.cbo.pojo.CboApprovalNodeInsEntity;
import com.topscomm.cbo.pojo.CboUserEntity;
import com.topscomm.cbo.service.ICboApprovalNodeInsService;
import com.topscomm.cbo.service.ICboUserService;
import com.topscomm.cbo.service.impl.CboApprovalNodeInsService;
import com.topscomm.cbo.service.impl.CboUserService;
import com.topscomm.mis.util.WorkCenterUtil;
import com.topscomm.pm.PmScript;
import com.topscomm.pm.PmSystemConst;
import com.topscomm.pm.pojo.PmContractApplicantChangeEntity;
import com.topscomm.pm.pojo.PmContractEntity;
import com.topscomm.pm.service.impl.PmBudgetApplyService;
import com.topscomm.pm.service.impl.PmContractApplicantChangeService;
import com.topscomm.pm.service.impl.PmContractService;
import com.topscomm.pub.util.ConvertUtil;
import com.topscomm.pub.util.SpringUtil;
import com.topscomm.pub.util.StringBufferProxy;
import com.topscomm.tap.ITapService;
import com.topscomm.tap.TapEntity;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: wuyuyang
 * @description:
 */
public class PmContractApplicantChangeScript extends PmScript {

    @Transactional(rollbackFor = Exception.class)
    public void changeApplicant(ITapService service, TapEntity entity) throws Exception {
        CboApprovalNodeInsEntity nodeins = (CboApprovalNodeInsEntity) entity;
        String sourceid = ConvertUtil.convertToString(nodeins.getSourceid());
        PmContractApplicantChangeService pmContractApplicantChangeService = (PmContractApplicantChangeService) SpringUtil.getBean("pmContractApplicantChangeService");
        PmContractService pmContractService = (PmContractService) SpringUtil.getBean("pmContractService");
        ICboApprovalNodeInsService cboApprovalNodeInsService = (CboApprovalNodeInsService) SpringUtil.getBean("cboApprovalNodeInsService");
        ICboUserService cboUserService = (CboUserService) SpringUtil.getBean("cboUserService");
        StringBufferProxy sql = new StringBufferProxy();

        // 1、获取项目合同单据
        PmContractApplicantChangeEntity pmContractApplicantChangeEntity = pmContractApplicantChangeService.queryById(sourceid);
        String contractId =  ConvertUtil.convertToString(pmContractApplicantChangeEntity.getProjectid());
        PmContractEntity pmContractEntity = pmContractService.queryById(contractId);

        // 2、修改项目合同申请人
        pmContractEntity.setUsercode(pmContractApplicantChangeEntity.getNewapplicantcode());
        pmContractEntity.setUsername(pmContractApplicantChangeEntity.getNewapplicant());
        pmContractService.updateEntity(pmContractEntity);

        // 3、判断项目合同当前单据的审批状态，修改所有需要当前申请人审批的节点
        if (pmContractEntity.getState() == PmSystemConst.INNAAPPROVAL) {
            // 获取原申请人id
            sql.appendSingle("code = '{0}'", pmContractApplicantChangeEntity.getOldapplicantcode());
            List<CboUserEntity> oldCboUserEntities = cboUserService.queryByWhere(sql.toString());
            long olduserid;
            if(oldCboUserEntities.size() > 0) {
                olduserid = oldCboUserEntities.get(0).getId();
            } else {
                throw new BusinessException("获取原申请人失败，请联系管理员");
            }
            // 获取现申请人id
            sql.clear();
            sql.appendSingle("code = '{0}'",pmContractApplicantChangeEntity.getNewapplicantcode());
            List<CboUserEntity> newCboUserEntities = cboUserService.queryByWhere(sql.toString());
            long newuserid;
            if(newCboUserEntities.size() > 0) {
                newuserid = newCboUserEntities.get(0).getId();
            } else {
                throw new BusinessException("获取现申请人失败，请联系管理员");
            }
            // 查找审批流程下所有需要原申请人审批的节点，将办理人改为新申请人
            sql.clear();
            sql.appendSingle("sourceid = '{0}' and sourcetype = '{1}' and state = '1' and approvaluserid = '{2}'",
                    pmContractEntity.getId(), pmContractEntity.getTableName(), olduserid);
            List<CboApprovalNodeInsEntity> cboApprovalNodeInsEntities = cboApprovalNodeInsService.queryByWhere(sql.toString());
            List<BasicEntity> basicEntities = new ArrayList<>();
            if (cboApprovalNodeInsEntities.size() > 0) {
                cboApprovalNodeInsEntities.forEach(cboApprovalNodeInsEntity -> {
                    cboApprovalNodeInsEntity.setApprovaluserid(newuserid);
                    basicEntities.add(cboApprovalNodeInsEntity);
                });
                cboApprovalNodeInsService.updateBatch(basicEntities);
                // 推送到权限中心
                WorkCenterUtil.sendFlowInsMqById(cboApprovalNodeInsEntities.get(0).getFlowinsid());
            }
        }

    }

}
